﻿using System;
using System.Collections.Generic;
using RockCorn.Internals;
using RockCorn.Objects.Moving;

namespace RockCorn.Objects
{
    class ShootingRacket : Racket
    {
        public new const string CollisionGroupString = "shooting racket";
        private int giftCollided = 0; // bullets count
        private bool shoot = false; // controlled by the ShootPlayerRacket method of ShootableRacketEngine via the Shoot method below

        public ShootingRacket(MatrixCoords topLeft, int width)
            : base(topLeft, width)
        {
            
        }

        public int Bullets
        {
            get { return this.giftCollided; }
        }

        public override IEnumerable<GameObject> ProduceObjects()
        {
            if(this.giftCollided>0 && shoot == true)
            {
                shoot = false;
                --this.giftCollided;
                List<GameObject> bullet = new List<GameObject>();
                bullet.Add(new Bullet(new MatrixCoords(this.topLeft.Row-1, this.topLeft.Col + this.Width/2)));
                this.body = this.GetRacketBody(this.Width); // reset body if needed
                return bullet;
            }
            else return new List<GameObject>();
        }

        protected override char[,] GetRacketBody(int width)
        {
            char[,] body = new char[1, width];

            if(giftCollided>0)
                for (int i = 0; i < width; i++)
                {
                    if (i == width / 2)
                    {
                        body[0, i] = '^';
                    }
                    else
                    body[0, i] = '=';
                }
            else
                for (int i = 0; i < width; i++)
                {
                    body[0, i] = '=';
                }

            return body;
        }

        public void Shoot()
        {
            if(this.giftCollided>0)
            this.shoot = true;
        }

        public override string GetCollisionGroupString()
        {
            return ShootingRacket.CollisionGroupString;
        }

        public override bool CanCollideWith(string otherCollisionGroupString)
        {
            return true;
        }

        public override void RespondToCollision(CollisionData collisionData)
        {
            if (collisionData.hitObjectsCollisionGroupStrings.Contains("gift"))
            {
                ++this.giftCollided;
                this.body = this.GetRacketBody(this.Width); // change body if needed
            }
            
        }
    }
}
